import './public-path'
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import 'element-plus/dist/index.css'
import ElementPlus from 'element-plus'
console.log(store.state.state);


// @ts-ignore
let instance: any = null

function render(props = {}) {
  // @ts-ignore
  const { container } = props
  // @ts-ignore
  instance = createApp(App).use(router).use(store).use(ElementPlus)
  instance.mount(container ? container.querySelector('#app') : '#app')
}
// @ts-ignore
if (!window.__POWERED_BY_QIANKUN__) {
  render()
}

// @ts-ignore
export async function bootstrap() {
  // console.log('%c%s', 'color: green;', 'vue3.0 app bootstraped')
}

function storeTest(props: any) {
  props.onGlobalStateChange &&
    props.onGlobalStateChange((value: any, prev: any) => {
      // console.log(
      //   `父组件的state值改变了,改成了${JSON.stringify(
      //     value
      //   )},老的值是${JSON.stringify(prev)}`
      // )
      store.state.state = Object.assign(store.state.state,value)
    })
  props.setGlobalState &&
    props.setGlobalState({
      ignore: props.name,
      user: {
        name: props.name,
      },
    })
}

export async function mount(props: any) {
  storeTest(props)
  // @ts-ignore
  render(props)
  instance.config.globalProperties.$onGlobalStateChange =
    props.onGlobalStateChange
  instance.config.globalProperties.$setGlobalState = props.setGlobalState
}

export async function unmount() {
  instance.unmount()
  instance._container.innerHTML = ''
  instance = null
  // @ts-ignore
  history?.destroy()
}
